import services from './service';
import _ from 'lodash';
import { Model, ItemUtils, unitsPrice } from '@/utils'
import { filterCondition, tableRadioCondition, filterCondition2 } from './config/filterConfig'
import { tableList, tableList2 } from './config/tableFields'
import { base } from '@/config';
import { message } from 'antd'
import pageConfig from './config/pageConfig';
const {namespace,listenRouter} = pageConfig
const initState = {
  filterCondition: filterCondition, // 列表-筛选
  tableRadioGroup: tableRadioCondition, // 列表-统计
  fields: tableList, // 列表-表格
  pagination: { // 分页
    ...base.initPage,
  },
  list: [], // 列表-数据
  filterParams: {}, // 筛选条件

  filterCondition2: filterCondition2, // 列表筛选2
  tableList2,
  list2: [], // 列表-数据
  filterParams2: {}, // 筛选条件
  pagination2: { // 分页
    ...base.initPage,
  },
}

export default Model.extend({
  namespace: namespace,
  state: _.cloneDeep(initState),
  subscriptions: {
    setup({ dispatch, listen }: any) {
      listen(listenRouter, () => {
        dispatch({
          type: 'updateState',
          payload: {
            ..._.cloneDeep(initState),
          }
        })

        dispatch({ type: 'fetchList' });
        dispatch({ type: 'fetchOptList' });
      });
    }
  },
  effects: {
    // 搜索列表 - 基本销售
    * fetchList({ payload }: any, { callWithSpinning, select, update }: any) {
      const {
        pagination: { ps, pn },
        filterParams,
      } = yield select((_: any) => _[namespace]);

      const searchParams = {
        ps,
        pn,
        ...filterParams,
        ...payload,
      }

      const data = yield callWithSpinning(services.fetchList, searchParams);

      if (data) {
        const { datas, pn, ps, tc } = data;
        yield update({
          list: datas,
          pagination: {
            ps,
            pn,
            total: tc,
            current: pn, // 组件需要
            pageSize: ps, // 组件需要
          },
        });
      }
    },

    // 搜索列表 - 销售标签
    * fetchOptList({ payload }: any, { callWithSpinning, select, update }: any) {
      const {
        pagination2: { ps, pn },
        filterParams2,
      } = yield select((_: any) => _[namespace]);

      const searchParams = {
        ps,
        pn,
        ...filterParams2,
        ...payload,
      }

      const data = yield callWithSpinning(services.fetchOptList, searchParams);

      if (data) {
        const { datas, pn, ps, tc } = data;
        yield update({
          list2: datas,
          pagination2: {
            ps,
            pn,
            total: tc,
            current: pn, // 组件需要
            pageSize: ps, // 组件需要
          },
        });
      }
    },

    // 上下架
    * fetchShelve({ payload }: any, { callWithConfirmLoading, put }: any) {
      yield callWithConfirmLoading(services.fetchShelve, {
        ...payload,
      }, {
        successMsg: '操作成功'
      })

      yield put('fetchOptList') // 更新列表
    },

    // 同步库存
    * fetchUpdateQty({ payload }: any, { callWithConfirmLoading, put }: any) {
      yield callWithConfirmLoading(services.fetchUpdateQty, {
        ...payload,
      }, {
        successMsg: '操作成功'
      })
    },

    // 发布
    * fetchPublish({ payload }: any, { callWithConfirmLoading, put }: any) {
      yield callWithConfirmLoading(services.fetchPublish, {
        ...payload,
      }, {
        successMsg: '发布成功'
      })

      yield put('fetchOptList') // 更新列表
    },
  },
  reducers: {
    // 更新筛选条件配置
    updateFilterConditon(state: any, {payload}: any) {
      const {
        filterCondition,
        varietySourceList,
      } = state;

      const _filterCondition = ItemUtils.getItemType(_.cloneDeep(filterCondition))
        .extend([
          {
            title: 'variety',
            selectCondition: varietySourceList,
          },
        ]).values()

      return {
        ...state,
        filterCondition: _filterCondition
      }
    },
  }
})
